package util;

/**
 *
 * @author Owner
 */

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TableButton extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
    
    public interface TableButtonPressedHandler{
	void onButtonPress(int row, int column);
    }
	
    public interface TableButtonCustomizer{
	void customize(JButton button, int row, int column);
    }

    private List<TableButtonPressedHandler> handlers;
    private Hashtable<Integer, JButton> buttons;
    private TableButtonCustomizer customizer;
    
    public TableButton(){
	handlers = new ArrayList<TableButtonPressedHandler>();
        buttons = new Hashtable<Integer, JButton>();
    }
    
    public TableButton(TableButtonCustomizer customizer){
    	this();
    	this.customizer = customizer;
    }

    public void addHandler(TableButtonPressedHandler handler){
        if (handlers != null){
            handlers.add(handler);
        }
    }

    public void removeHandler(TableButtonPressedHandler handler){
        if (handlers != null){
            handlers.remove(handler);
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focus, final int row, final int column) {
        JButton button = null;
        if(buttons.containsKey(row)){
            button = buttons.get(row);
        } else {
            button = new JButton();
            if(customizer != null){
            	customizer.customize(button, row, column);
            }
            if(value != null && value instanceof String){
                button.setText((String)value);
            }
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    if(handlers != null){
                        for(TableButtonPressedHandler handler : handlers){
                            handler.onButtonPress(row, column);
                        }
                    }
                }
            });
            buttons.put(row, button);
        }

        return button;
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean selected, int row, int column){
        JButton button = null;
        if(buttons.containsKey(row)){
            button = buttons.get(row);
        } else {
            button = new JButton();
            if(customizer != null){
            	customizer.customize(button, row, column);
            }
	    
            if(value != null && value instanceof String){
                button.setText((String)value);
            }

            buttons.put(row, button);
        }
        return button;
    }
   
    public void setButtonText(int row, String text){
        JButton button = null;
        if(buttons.containsKey(row)){
            button = buttons.get(row);
            button.setText(text);
        }
    }

    public Object getCellEditorValue() {
        return null;
    }

    public void dispose(){
	if (handlers != null){
	    handlers.clear();
	}
    }
}
